package com.tc.dispatch.web.common.login;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import com.tc.dispatch.web.controller.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: ZJU</p>
 * @author DxFeng
 * @version 1.0
 */

public class login extends HttpServlet
{
    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html; charset=GBK");
        PrintWriter out = response.getWriter();

        // �ѵ�ǰ�Ự����
        request.getSession().invalidate();

        // DefaultSessionListener ���ٽ�һ���µ� UserState������ userState.getUserId() == null
        UserState userState = (UserState) request.getSession().getAttribute("userState");

        if(userState.isLoggedIn()){
          //out.println("��¼ʧ��");

          out.println("<script language=\"JavaScript\">");
          out.println("if(window.screen.width >= 1020)");
          out.println("portalWindow = window.open('../error/error.jsp?code=3', 'portalWindow', 'toolbar=no, menubar=no, location=no, directories=no, status=no, scrollbars=no, resizable=yes, width=1014, height=710, top=0, left=0');");
          out.println("else");
          out.println("portalWindow = window.open('../error/error.jsp?code=3', 'portalWindow', 'toolbar=no, menubar=no, location=no, directories=no, status=no, scrollbars=no, resizable=yes, width=790, height=542, top=0, left=0');");
          out.println("window.open('login.jsp', '_top');");
          out.println("portalWindow.focus();");
          out.println("</script>");
          return;
        }
        else{

          String user_id = request.getParameter("user_id");
          String password = request.getParameter("password");
          if (userState.login(user_id, password)) {
            response.sendRedirect("../portal/index.jsp");

     
          }
          else {

            out.println("<script language=\"JavaScript\">");
            out.println("if(window.screen.width >= 1020)");
            out.println("portalWindow = window.open('../error/error.jsp?code=7', 'portalWindow', 'toolbar=no, menubar=no, location=no, directories=no, status=no, scrollbars=no, resizable=yes, width=1014, height=710, top=0, left=0');");
            out.println("else");
            out.println("portalWindow = window.open('../error/error.jsp?code=7', 'portalWindow', 'toolbar=no, menubar=no, location=no, directories=no, status=no, scrollbars=no, resizable=yes, width=790, height=542, top=0, left=0');");
            out.println("window.open('login.jsp', '_top');");
            out.println("portalWindow.focus();");
            out.println("</script>");
          }
        }
    }
}
